//
//  ServiceConfigVC.m
//  YHEduCustomer
//
//  Created by 米翊米 on 2018/5/23.
//  Copyright © 2018年 米翊米. All rights reserved.
//

#import "ServiceConfigVC.h"

@interface ServiceConfigVC ()

@property (weak, nonatomic) IBOutlet UIButton *TestButton;
@property (weak, nonatomic) IBOutlet UIButton *ReleaseButton;
@property (weak, nonatomic) IBOutlet UITextField *serviceField;
@property (weak, nonatomic) IBOutlet UIButton *CustomButton;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;

@end

@implementation ServiceConfigVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self navigationBarColor:nil shadow:YES];
    self.navigationItem.title = @"服务端配置";
    [self addLeftItem:YES];
    
    [UserDefault removeObjectForKey:@"shake"];
    if (![UserDefault objectForKey:ServiceKey]) {
        [UserDefault setObject:@(ServiceRelease) forKey:ServiceKey];
    }
    ServiceType type = [[UserDefault objectForKey:ServiceKey] integerValue];
    if (type == ServiceTest) {
        self.tipsLabel.text = StringFormat( @"当前环境为测试:\n %@" , DevelopmenteHost);
        ViewRadius(self.TestButton, 4);
        ViewBorder(self.TestButton, 1, kColorSkin);
        HOST = DevelopmenteHost;
    } else if (type == ServiceRelease) {
        self.tipsLabel.text = StringFormat( @"当前环境为正式:\n %@" , DistributeHost);
        ViewRadius(self.ReleaseButton, 4);
        ViewBorder(self.ReleaseButton, 1, kColorSkin);
        HOST = DistributeHost;
    } else if (type == ServiceCustom) {
        self.tipsLabel.text = StringFormat( @"当前环境为自定义:\n %@" , [UserDefault objectForKey:Service]);
        self.serviceField.text = [UserDefault objectForKey:Service];
        ViewRadius(self.CustomButton, 4);
        ViewBorder(self.CustomButton, 1, kColorSkin);
        HOST = [UserDefault objectForKey:Service];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (void)leftAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)testAction:(id)sender {
    HOST = DevelopmenteHost;
    
    [self skipLoginVC];
    [UserDefault setObject:@(ServiceTest) forKey:ServiceKey];
}

- (IBAction)releaseAction:(id)sender {
    HOST = DistributeHost;
    
    [self skipLoginVC];
    [UserDefault setObject:@(ServiceRelease) forKey:ServiceKey];
}

- (IBAction)coustomAction:(id)sender {
    NSString *text = self.serviceField.text;
    if ([text spaceFilter].length < 5) {
        [[QZHHUD HUD] textHUDWithMessage:@"请输入正确地址" afterDelay:1.0];
        return;
    }
    if ([text hasPrefix:@"https://"] || [text hasPrefix:@"http://"]) {
        HOST = self.serviceField.text;
    } else {
        HOST = StringFormat(@"http://%@", self.serviceField.text);
    }
    [UserDefault setObject:@(ServiceCustom) forKey:ServiceKey];
    [UserDefault setObject:self.serviceField.text forKey:Service];
    [self skipLoginVC];
}

- (void)skipLoginVC {
    [QZHDataCache removeAll];
    [UserModel setNil];
    [RootDelegate rootVC];
}

@end
